package com.pickgliss.ui.text
{
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.ComponentSetting;
	import com.pickgliss.ui.core.Disposeable;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.events.FocusEvent;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 *
	 * @author Herman
	 * 支持跳帧的文本 跳帧的实现主要是文本的滤镜不一样
	 *
	 */
	public class FilterFrameText extends TextField implements Disposeable
	{
		public static const P_frameFilters:String = "frameFilters";

		public function FilterFrameText()
		{
			super();
			autoSize = TextFieldAutoSize.LEFT;
			mouseEnabled = false;
			_currentFrameIndex = 1;
			_frameTextFormat = new Vector.<TextFormat>();
		}
		
		public static var isInputChangeWmode:Boolean = true;
		protected var _currentFrameIndex:int;
		protected var _filterString:String;
		protected var _frameFilter:Array;
		protected var _frameTextFormat:Vector.<TextFormat>;
		protected var _id:int;
		protected var _width:Number;
		protected var _height:Number;
		protected var _isAutoFitLength:Boolean = false;
		public var stylename:String;

		/**
		 *
		 * @param stylename 文本格式的样式
		 * 通过ComponentFactory.Instance.model.getSet 获取
		 *
		 */
		protected var _textFormatStyle:String;

		public function dispose():void
		{
			_frameFilter = null;
			if (parent)
				parent.removeChild(this);
			ComponentFactory.Instance.removeComponent(_id);
		}


		public function set filterString(filters:String):void
		{
			if (_filterString == filters)
				return;
			_filterString = filters;
			frameFilters = ComponentFactory.Instance.creatFrameFilters(_filterString);
			setFrame(1);
		}

		public function set frameFilters(filter:Array):void
		{
			if (_frameFilter == filter)
				return;
			_frameFilter = filter;
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}

		/**
		 *设置焦点
		 */
		public function setFocus():void
		{
			StageReferance.stage.focus = this;
		}

		public function setFrame(frameIndex:int):void
		{
			_currentFrameIndex = frameIndex;
			if (_frameFilter != null && _frameFilter.length >= frameIndex)
			{
				filters = _frameFilter[frameIndex - 1];
			}

			if (_frameTextFormat != null && _frameTextFormat.length >= frameIndex)
			{
				textFormat = _frameTextFormat[frameIndex - 1];
			}
		}

		protected function set textFormat(tf:TextFormat):void
		{
			setTextFormat(tf);
			defaultTextFormat = tf;
		}

		public function set textFormatStyle(stylename:String):void
		{
			if (_textFormatStyle == stylename)
				return;
			_textFormatStyle = stylename;
			var frameTextFormats:Array = ComponentFactory.parasArgs(_textFormatStyle);
			_frameTextFormat = new Vector.<TextFormat>();
			for (var i:int = 0;i < frameTextFormats.length;i++)
			{
				var format:TextFormat = ComponentFactory.Instance.model.getSet(frameTextFormats[i]);
				_frameTextFormat.push(format);
			}
			setFrame(1);
		}

		public function set isAutoFitLength(value:Boolean):void{
			_isAutoFitLength = value;
		}
		
		override public function set visible(value:Boolean):void
		{
			super.visible = value;
		}
		
		override public function set text(value:String):void
		{
			super.text = value;
			if(_isAutoFitLength && textWidth > width){
				var tempIndex:int = getCharIndexAtPoint(width - 22, 5);
				super.text = text.substring(0,tempIndex)+"...";
//				super.width = _width;
			} 
			
			if (_width > 0 || _height > 0)
			{
				if (wordWrap == true)
				{
					width = _width;
				}
			}
			setFrame(_currentFrameIndex);
		}

		override public function set htmlText(value:String):void
		{
			if (_width > 0 || _height > 0)
			{
				if (wordWrap == true)
				{
					width = _width;
				}
			}
			setFrame(_currentFrameIndex);
			super.htmlText = value;
		}

		override public function set width(value:Number):void
		{
			_width = value;
			super.width = value;
		}

		override public function set height(value:Number):void
		{
			_height = value;
			super.height = value;
		}

		override public function set type(value:String):void
		{
			if (value == TextFieldType.INPUT)
			{
				mouseEnabled = true;
			}
			super.type = value;
		}
	}
}